package leetcode.editor.cn.q1_300.q150;

/**
 * @author qidi
 * @date 2022-01-21 08:45:53
 */
class Q0154_FindMinimumInRotatedSortedArrayIi {

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int findMin(int[] nums) {
            int l = 0;
            int r = nums.length - 1;
            while (l < r && nums[l] >= nums[r]) {
                int m = (r + l) / 2;
                if (nums[l] == nums[m]) {
                    while (l < r) {
                        l++;
                        if (nums[l] != nums[l - 1]) break;
                    }
                    continue;
                }
                m = (r + l - 1) / 2;
                if (nums[l] > nums[m]) {
                    r = m;
                } else {
                    l = m + 1;
                }
            }

            return nums[l];
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)


    public static void main(String[] args) {
        Solution solution = new Q0154_FindMinimumInRotatedSortedArrayIi().new Solution();
        System.out.println(solution.findMin(new int[]{3, 4, 5, 1}));
        System.out.println(solution.findMin(new int[]{2, 2, 2, 0, 1}));
        System.out.println(solution.findMin(new int[]{1}));
        System.out.println(solution.findMin(new int[]{1, 1, 2, 0, 1, 1}));
        System.out.println(solution.findMin(new int[]{1, 1, 1, 1, 2, 1, 1, 1}));
    }
}